package com.own.business.exam.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.own.business.exam.business.service.ExamCustomerRankService;
import com.own.business.exam.business.service.ExamRuleService;
import com.own.business.exam.business.service.ExamService;
import com.own.business.exam.common.exception.ExamException;
import com.own.business.exam.entity.bo.ExamBo;
import com.own.business.exam.entity.map.ExamMapVo;
import com.own.business.exam.entity.po.Exam;
import com.own.business.exam.entity.po.ExamCustomerRank;
import com.own.business.exam.entity.po.ExamRule;
import com.own.business.exam.entity.query.ExamQuery;
import com.own.business.exam.entity.vo.ExamVo;
import com.own.business.exam.mapper.ExamMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import com.own.component.common.model.UpdateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ExamServiceImpl
 *
 * @author chenxueli
 * @date 2021/05/18 14:24:25
 */
@Slf4j
@Service
public class ExamServiceImpl extends AbstractBaseService<
        Exam,
        ExamBo,
        ExamVo,
        ExamMapVo,
        ExamQuery,
        ExamMapper
        > implements ExamService {

    @Resource
    private ExamRuleService examRuleService;
    @Resource
    private ExamCustomerRankService examCustomerRankService;

    /**
     * 获取查询条件
     *
     * @param query 查询条件
     * @return 查询条件对象
     */
    @Override
    public LambdaQueryWrapper<Exam> lambdaQueryWrapper(ExamQuery query) {
        return super.lambdaQueryWrapper(query)
                .like(StringUtils.isNotEmpty(query.getTitle()), Exam::getTitle, query.getTitle())
                .eq(query.getType() != null, Exam::getType, query.getType())
                .orderByDesc(Exam::getGmtCreate);
    }

    /**
     * 查询所有的数据
     *
     * @param query 查询条件
     * @return 所有的列表数据
     */
    @Override
    public List<ExamVo> all(ExamQuery query) {
        return listByWrapper(lambdaQueryWrapper(query)).stream().map(ExamVo::new).collect(Collectors.toList());
    }

    /**
     * 修改信息
     *
     * @param exam 修改对象
     * @return 修改标识
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<ExamVo> update(Exam exam) {
        if (exam.getEndTime().isAfter(exam.getStartTime())) {
            throw new ExamException("开始时间不能在结束时间之后");
        }
        return super.update(exam);
    }

    /**
     * 删除信息
     *
     * @param pk 主键id
     * @return 删除标识
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UpdateModel<Boolean> delete(Long pk) {
        return new UpdateModel<>().check(
                examRuleService.delete(ExamRule::getFkExamId, pk),
                examCustomerRankService.delete(ExamCustomerRank::getFkExamId, pk),
                super.delete(pk)
        );
    }
}
